// pages/likeCity/likeCity.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: 'https://armeco.happydoit.com/upload', // 服务器图片路径
    photoList: [], // 城市照片墙
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getCityPhotoList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    return {
        title: '让邓伦来我的芯愿城市，就靠你的助力了！',
        path: '/pages/stage/showCity/showCity',
        imageUrl: that.data.imageUrl + '/cityimages/share-img.png'
      }
  },

  // 获取城市照片墙
  getCityPhotoList: function () {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    wx.request({
      url: app.globalData.requestUrl + '/api/city/cityPhotosList',
      data: {},
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": "JSESSIONID=" + app.globalData.jessionId
      },
      success: function (res) {
        if (res.data.code == 'success') {
          wx.hideLoading();
          that.setData({
            photoList: res.data.data
          })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true
          })
        }
      }
    })
  },

  // 图片加载失败
  errImg: function (e) {
    console.log(e)
  },

  // 点赞
  giveLike: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let photoList = that.data.photoList;
    wx.request({
      url: app.globalData.requestUrl + '/api/city/voteCity',
      data: {
        effectid: id
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": "JSESSIONID=" + app.globalData.jessionId
      },
      success: function (res) {
        if (res.data.code == 'success') {
          for (let i = 0; i < photoList.length; i++) {
            if (photoList[i].id === id) {
              photoList[i].praise++;
              photoList[i].status = 1;
            }
          }
          that.setData({
            photoList: photoList
          })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true
          })
        }
      }
    })
  },

  openRule() {
    this.setData({
      showRule: true
    })
  },

  onGetCode: function (e) {
    this.setData({
      showRule: e.detail.val
    })
  }
})